package com.controller
{
	
	import com.events.CurrencyMasterEvent;
	import com.model.valueObjects.VoCurrencyDetail;
	import com.model.valueObjects.VoCurrencyHeader;
	import com.service.CurrencyMaster;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.managers.CursorManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	public class CurrencyMasterController extends AbstractController
	{
		import com.model.Model;
		
		[Inject]
		public var currencyMasterService:CurrencyMaster;
		
		
		//[PostConstruct]
		public function CurrencyMasterController()
		{
			super();	
		}
		
		[EventHandler(event="CurrencyMasterEvent.GET_ALL")]
		public function getAllCurrency():void{
			model.currentAdminTableIndex=8;
			sh.executeServiceCall(currencyMasterService.getAllCurrency(),getAllResult, handleFault );
		}
		private function getAllResult(event:ResultEvent):void
		{
			model.currencyMasterList=event.result as ArrayCollection;
			if(model.currentAdminTableIndex==8)
			{
				CursorManager.removeBusyCursor();
				model.busyCurrsorOn=true;	
			}
		}
		
		[EventHandler(event="CurrencyMasterEvent.GET_ALL_DETAILS")]
		public function getAllCurrencyDetails():void{
			sh.executeServiceCall(currencyMasterService.getAllCurrencyDetails(),getAllDetailsResult, handleFault );	
		}
		private function getAllDetailsResult(event:ResultEvent):void
		{
			model.currencyDetailList=event.result as ArrayCollection;	
		}
		
		[EventHandler(event="CurrencyMasterEvent.GET_ALL_DET_SERIALS")]
		public function getAllCurrencyDetSerials():void{
			sh.executeServiceCall(currencyMasterService.getAllCurrencyDetSerials(),getAllDetSerialResult, handleFault );
		}
		private function getAllDetSerialResult(event:ResultEvent):void
		{
			model.currencyDetSerialList=event.result as ArrayCollection;
		}
		
		[EventHandler(event="CurrencyMasterEvent.ADD_CURRENCY", properties="currencyHeaderVo")]
		public function addNewCurrency(currencyHeaderVo:VoCurrencyHeader):void
		{
			sh.executeServiceCall(currencyMasterService.addNewCurrency(currencyHeaderVo), addNewCurrencyResult, handleFault );	
		}
		private function addNewCurrencyResult(event:ResultEvent):void
		{
			getAllCurrency();
			getAllCurrencyCountry();
			CursorManager.removeBusyCursor();
			Alert.show("New Currency  Added " ,"Currency");
		}
		
		[EventHandler(event="CurrencyMasterEvent.GET_CURRENCY_COUNTRY")]
		public function getAllCurrencyCountry():void{
			sh.executeServiceCall(currencyMasterService.getCountriesNotHaveCurrency(),getAllCurrencyCountryResult, handleFault );
		}		
		private function getAllCurrencyCountryResult(event:ResultEvent):void
		{
			model.countryCurrencyList=event.result as ArrayCollection;
		}
		
		[EventHandler(event="CurrencyMasterEvent.ADD_CURRENCY_DETAIL", properties="currencyDetailsVo")]
		public function addCurrencyDetail(currencyDetailsVo:VoCurrencyDetail):void
		{
			sh.executeServiceCall(currencyMasterService.addCurrencyDetails(currencyDetailsVo), addCurrencyDetailResult, handleFault );	
		}		
		private function addCurrencyDetailResult(event:ResultEvent):void
		{
			getAllCurrencyDetails();
			CursorManager.removeBusyCursor();
			Alert.show("New Currency value Added " ,"Currency");
		}
		
		[EventHandler(event="CurrencyMasterEvent.UPDATE", properties="currencyHeaderVo")]
		public function updatecurrency(currencyHeaderVo:VoCurrencyHeader):void
		{
			sh.executeServiceCall(currencyMasterService.editCurrencyHeader(currencyHeaderVo), updateResult, handleFault );	
		}
		private function updateResult(event:ResultEvent):void
		{
			getAllCurrency();
			model.currentForm=1;
			CursorManager.removeBusyCursor();
			Alert.show("Details Updated ","Currency");
		}
		
		[EventHandler(event="CurrencyMasterEvent.DELETE", properties="currencySerialNo")]
		public function deleteR(currencySerialNo:Number):void
		{
			sh.executeServiceCall(currencyMasterService.deleteCurrency(currencySerialNo), deleteResult, handleFault );
		}
		private function deleteResult(event:ResultEvent):void
		{
			getAllCurrency();
			model.currentForm=1;
			CursorManager.removeBusyCursor();
			Alert.show("Record Deleted " ,"Currency");
		}
		
		private function handleFault(event:FaultEvent):void
		{
			Alert.show( event.fault.faultDetail, "Error" );
		}
	}
}